<template>
  <div>
    <el-form ref="form" :model="searchForm" :inline="true">
  <el-form-item>
    <el-input v-model="searchForm.username" placeholder="请输入要搜索的黑名单用户"></el-input>
  </el-form-item>
  <el-form-item >
    <el-button @click="searchBlacklistInfo">搜索</el-button>
    <el-button type="primary" @click="dialogFormVisible=true" style="margin-right:5px ;">新建黑名单用户</el-button>
    <el-popconfirm title="确定将用户移出黑名单吗吗？" @confirm="deleteBlacklist">
    <el-button slot="reference" type="danger" :disabled="deleteAllBtnStu">批量移出</el-button>
   </el-popconfirm>
  </el-form-item>
 </el-form>
 <el-table :data="tableData" border stripe  style="width: 100%; margin-bottom:20px;" :cell-style="{padding:'2px'}" @selection-change="selectionChange">
    <el-table-column type="selection" width="50">
    </el-table-column>
    <el-table-column label="头像" width="55">
      <template slot-scope="scope">
        <el-avatar size="small" :src="scope.row.avatar"></el-avatar>
      </template>
    </el-table-column>
    <el-table-column label="用户名" width="120" prop="name">
      </el-table-column>
      <el-table-column label="角色" width="200">
        <template slot-scope="scope">
        <el-tag size="mini" v-for="(blacklist,index) in scope.row.blacklists" :key="blacklist.id" style="margin-right: 2px;">
          {{ blacklist.name }}
        </el-tag>
       </template>
      </el-table-column>
      <el-table-column label="邮箱"  prop="email" width="180">
      </el-table-column>
      <el-table-column label="手机号" prop="tel" width="120">
      </el-table-column>
      <el-table-column label="状态" width="70">
        <template slot-scope="scope">
        <!-- <el-tag type="success" size="mini">正常</el-tag> -->
        <el-tag size="mini" v-if="scope.row.statu===1" type="success">正常</el-tag>
        <el-tag size="mini" v-else-if="scope.row.statu===0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间"  width="200" prop="created">
        <template slot-scope="scope">
          {{ scope.row.created | fmtDate }}
        </template>
      </el-table-column>
      <el-table-column label="加入黑名单时间"  width="200" prop="blocked">
        <template slot-scope="scope">
          {{ scope.row.blocked | fmtDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
        <el-popconfirm title="确定将该用户移出黑名单吗？" @confirm="deleteBlacklist(scope.row.id)">
        <el-button slot="reference" type="text">移出黑名单</el-button>
        </el-popconfirm>
        </template>
      </el-table-column>
  </el-table>
  <!-- 显示页码 -->
  <el-pagination background layout="total, sizes,prev, pager, next,jumper" :total="total" :page-size="size"
  :page-sizes="[5,10,15,20,25]" :current-page="current">
  </el-pagination>
  <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="600px" :before-close="handleClose">
   <el-form :model="BlacklistForm" ref="BlacklistForm" label-width="100px" :rules="rules">
     <el-form-item label="用户头像" label-width="100px">
      <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess"  :headers="headers">
  <img v-if="BlacklistForm.avatar" :src="BlacklistForm.avatar" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
     </el-form-item>
     <el-form-item label="用户名" label-width="100px" prop="name">
      <el-input v-model="BlacklistForm.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" label-width="100px" prop="email">
      <el-input v-model="BlacklistForm.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="手机" label-width="100px" prop="tel">
      <el-input v-model="BlacklistForm.tel" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="状态" label-width="100px">
      <el-radio-group v-model="BlacklistForm.statu">
        <el-radio :label="0">禁用</el-radio>
        <el-radio :label="1" disabled="true">正常</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('BlacklistForm')">加入黑名单</el-button>
      <el-button  @click="resetForm('BlacklistForm')">重置</el-button>
    </el-form-item>
   </el-form>
   </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selection:[],
      dialogFormVisible:false,
      tableData:[],
      total:0, 
      size:10,   
      current:1,  
      deleteAllBtnStu:true,
      searchForm:{},
      BlacklistForm:{
        avatar:'',
        name:'',
        email:'',
        tel:'',
        statu:0,
      },
      headers:{
        token:localStorage.getItem('token')
      },
      rules:{
            name:[
                {required:true,message:'请输入用户名',trigger:'blur'},
            ],
            email:[
                {required:true,message:'请输入邮箱',trigger:'blur'},
            ],
            tel:[
                {required:true,message:'请输入手机',trigger:'blur'},
                {min:11,max:11,message:'手机号的长度为11个字符',trigger:'blur'}
            ]
        }
    }
  },
  created(){
    this.loadBlacklistList();
  },
  filters:{
    fmtDate:(value)=> {
      let date=new Date(value)
      let y=date.getFullYear()
      let M=(date.getMonth()+1).toString().padStart(2,'0')
      let d=date.getDate().toString().padStart(2,'0')
      let h=date.getHours().toString().padStart(2,'0')
      let m=date.getMinutes().toString().padStart(2,'0')
      let s=date.getSeconds().toString().padStart(2,'0')

      return `${y}年${M}月${d}日 ${h}:${m}:${s}`
    }
  },
  methods: {
    loadBlacklistList(){
      this.$axios.get('/system/blacklist/list',{
        current:this.current,
        size:this.size,
      }).then(response=>{
        this.tableData= response.data.data.records
        this.total=response.data.data.total
        this.size=response.data.data.size
        this.current=response.data.data.current
      })
    },
    deleteBlacklist(id){
      let ids=[]
      id?ids.push(id):this.selection.forEach(u=>{
        ids.push(u.id)
      })
      this.$axios.post('/system/blacklist/delete',ids).then(response=>{
        this.$message({
            message:response.data.message,
            type:'success',
            showClose:true,
           onClose:()=>{
            this.loadBlacklistList()
           }
        });
      })
    },
    handleClose(){
      this.BlacklistForm={}
      this.dialogFormVisible=false
    },
    handleAvatarSuccess(response,file){
      this.BlacklistForm.avatar= response.data
    },
    submitForm(formName){
      this.$refs.BlacklistForm.validate(valid=>{
        if(valid){

          this.$axios.post('/system/blacklist/save',this.BlacklistForm).then(response=>{
            this.$message({
            message:response.data.message,
            type:'success',
            // showClose:true,
            duration:1500,
           onClose:()=>{
            this.loadBlacklistList()
            this.dialogFormVisible=false
            this.resetForm(formName)
           }
        });
          })
        }
      })
    },
    searchBlacklistInfo(){

    },
    resetForm(formName){
      this.BlacklistForm={}
      this.$refs.BlacklistForm.resetFields()
    },
    selectionChange(value){
      this.selection=value
      if(this.selection){
        //有数据便是有选中的
        this.deleteAllBtnStu=(this.selection.length===0)  //或false
      }
    }
  },
  components: {
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #999;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}
</style>
